package com.polaris.live.widget.circle

import android.animation.Animator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.blankj.utilcode.util.ColorUtils
import com.polaris.live.R

/**
 * CircleView
 *
 * @author Created by 半仙 on 2023/10/27/027 19:34
 */

class CircleView(context: Context, attrs: AttributeSet) : View(context, attrs) {

    private var progress = 0f
    private var animator: ValueAnimator? = null

    private val paint = Paint()
    private val backgroundPaint = Paint()

    private var mStrokeWidth = 10f
    private var mPaintingColor: Int? = null//绘画颜色
    private var mBackColor: Int? = null//背景颜色

    var onAnimationEnd: (() -> Unit)? = null

    fun setAnimationCallback(callback: () -> Unit) {
        onAnimationEnd = callback
    }

    var onAnimationStart: (() -> Unit)? = null

    fun setAnimationStartCallback(callback: () -> Unit) {
        onAnimationStart = callback
    }

    var isCanceled = false

    /**
     * time 总时长
     * strokeWidth 边框宽度
     * paintingColor绘画的颜色
     * backColor底色
     * startValues起点位置
     */
    fun anewRun(
        time: Long = 10000,
        strokeWidth: Float = 10f,
        paintingColor: Int? = null,
        backColor: Int? = null,
        startValues: Float = 0f,
        clockwise: Boolean = false, //顺时针
    ) {
        mPaintingColor = paintingColor
        mBackColor = backColor
        mStrokeWidth = strokeWidth

        if (animator?.isRunning == true) {
            animator?.cancel()
        }
        val va = if (clockwise) 360f else -360f
        animator = ValueAnimator.ofFloat(startValues, va).apply {
            duration = time
            repeatCount = 0
            addUpdateListener { animation ->
                progress = animation.animatedValue as Float
                invalidate() // 刷新视图，重新绘制圆环
            }
            addListener(object : Animator.AnimatorListener {
                override fun onAnimationStart(animation: Animator) {
                    onAnimationStart?.invoke()
                }

                override fun onAnimationEnd(animation: Animator) {
                    if (!isCanceled) {
                        onAnimationEnd?.invoke()
                    }
                    isCanceled = false
                }

                override fun onAnimationCancel(animation: Animator) {
                    isCanceled = true
                }

                override fun onAnimationRepeat(animation: Animator) {
                }
            })
            start()
        }
    }

    fun setProgress(progress: Float, strokeWidth: Float? = null, paintingColor: Int? = null, backColor: Int? = null) {

        paintingColor?.let { mPaintingColor = paintingColor }
        backColor?.let { mBackColor = backColor }
        strokeWidth?.let { mStrokeWidth = strokeWidth }

        if (animator?.isRunning == true) {
            animator?.cancel()
        }
        this.progress = progress
        invalidate()
    }

    //是否正在进行中
    fun isRunning() = animator?.isRunning ?: false

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        val centerX = width / 2f
        val centerY = height / 2f
        val radius = (width / 2f) - 10f

        paint.apply {
            style = Paint.Style.STROKE
            strokeWidth = mStrokeWidth
        }
        val pColor = mPaintingColor
        paint.color = if (pColor != null) pColor else ColorUtils.getColor(R.color.color_fff1b1)

        val bColor = mBackColor
        backgroundPaint.apply {
            style = Paint.Style.STROKE
            strokeWidth = mStrokeWidth
            color = if (bColor != null) bColor else ColorUtils.getColor(R.color.color_000000_30)
        }

        // 绘制背景圆弧
        canvas.drawArc(
            centerX - radius, centerY - radius, centerX + radius, centerY + radius,
            -90f, 360f, false, backgroundPaint
        )


        canvas.drawArc(
            centerX - radius, centerY - radius, centerX + radius, centerY + radius,
            -90f, progress, false, paint
        ) // 绘制圆弧
    }

    override fun onDetachedFromWindow() {
        animator?.cancel()
        animator = null
        super.onDetachedFromWindow()
    }
}